<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['diy']=$this->plugin_setting('diy');
//日期 今天，未来七天
$data=array();
$data['0']=date("Y-m-d",time("day")); 
$data['1']=date("Y-m-d",strtotime("+1 day")); 
$data['2']=date("Y-m-d",strtotime("+2 day")); 
$data['3']=date("Y-m-d",strtotime("+3 day")); 
$data['4']=date("Y-m-d",strtotime("+4 day")); 
$data['5']=date("Y-m-d",strtotime("+5 day")); 
$data['6']=date("Y-m-d",strtotime("+6 day"));
if($_RQ['op']=='main'){	
	$serve=pdo_get('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['serve_id']),array('id','title','serve_time','package','serve_types'));
	$worker=pdo_get('jy_meye_store_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['worker_id']),array('name','headimg','sex','age','years'));
	$serve['serve_time']=json_decode($serve['serve_time'],true);
	$serve_time=$serve['serve_time'][intval($_RQ['timeid'])];
	$serve['package']=json_decode($serve['package'],true);
	$package=$serve['package'][intval($_RQ['packageid'])];
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$return['day']=$data[$_RQ['dayid']];
	$return['package']=$package;
	if($serve['serve_types']){
		$newserve_types=array();
		if(strpos($serve['serve_types'],';0;')!== false){ 
			$newserve_types[0]=array('id'=>0,'title'=>'到店服务');
			$serve_type='0';
		}
		if(strpos($serve['serve_types'],';1;')!== false){ 
			$newserve_types[1]=array('id'=>1,'title'=>'上门服务');
			$serve_type=1;
		}
		$serve['serve_types']=$newserve_types;
	}else{
		$serve['serve_types']=array(
			array('id'=>'0','title'=>'到店服务'),
			array('id'=>'1','title'=>'上门服务')
		);
	}
	$return['serve']=$serve;
	$return['worker']=$worker;
	$return['serve_time']=$serve_time;
	//次卡信息：
	$timesorder=pdo_get('jy_meye_timeserve_order',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'lasttimes >'=>0,'serve_id'=>$serve['id']),array('id','lasttimes','goods_id'));
	$timeserve=pdo_get('jy_meye_timeserve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$timesorder['goods_id']),array('title'));
	$return['timesorder']=$timesorder;
	$return['timeserve']=$timeserve;
	$totalprice=$package['package_price'];
	if($timesorder['id']){
		$totalprice=0.00;
	}
	$return['post']=array(
		'timesorderid'=>intval($timesorder['id']),
		'serve_id'=>intval($_RQ['serve_id']),
		'timeid'=>intval($_RQ['timeid']),
		'packageid'=>intval($_RQ['packageid']),
		'store_id'=>intval($_RQ['store_id']),
		'worker_id'=>intval($_RQ['worker_id']),
		'dayid'=>intval($_RQ['dayid']),
		'price'=>$package['package_price'],
		'totalprice'=>$totalprice,
		'username'=>$member['nickname'],
		'mobile'=>$member['mobile'],
		'serve_type'=>$serve_type,
	);
	$return['store']=pdo_get('jy_meye_store',array('id'=>$return['post']['store_id']));
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$serve_id = $this->session('serve_id');
	if($postdata){
		if(!$postdata['username']){
			exi('请输入联系人姓名','error');
		}
		if(!$postdata['mobile']){
			exi('请输入联系人电话','error');
		}
		if(!preg_match("/^1[345678]{1}\d{9}$/",$postdata['mobile'])){  	 
			exi('请输入正确的手机号码','error');
		} 
		if($postdata['fileds']){
			foreach ($postdata['fileds'] as $item) {
				if($item['ismust']==1 && empty($item['data'])){
					exi('请完善'.$item['title'].'内容后再提交','error');
				}
				if($item['type']==7){
					$item['data']=$item['data']['province'].$item['data']['city'].$item['data']['district'].$item['data']['xiangxi'];	
				}
			}
		}
		$package=pdo_getcolumn('jy_meye_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$serve_id),'package');
		$package=json_decode($package,true);
		$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'worker_id'=>$postdata['worker_id'],
				'store_id'=>$postdata['store_id'],
				'serve_id'=>$postdata['serve_id'],
				'package'=>ijson_encode($package[$postdata['packageid']]),
				'timeid'=>$postdata['timeid'],
				'day'=>$data[$postdata['dayid']],
				'address'=>$postdata['address'],
				'username'=>$postdata['username'],
				'mobile'=>$postdata['mobile'],
				'remark'=>$postdata['remark'],
				'ordersn'=>get_ordersn('jy_meye_server_order','ordersn'),
				'fileds'=>ijson_encode($postdata['fileds']),
				'money'=>$postdata['price'],
				'pay'=>$postdata['totalprice'],
				'createtime'=>TIMESTAMP,
				'timesorderid'=>intval($postdata['timesorderid']),
				'serve_type'=>intval($postdata['serve_type']),
		);
		pdo_insert('jy_meye_server_order',$insert);
		$id=pdo_insertid();
		$params=array(
				'id'=>$id,
				'title'=>'预约订单',
				'fee'=>$postdata['totalprice'],
				'plugin'=>'jy_meye',
				//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
				'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
				'back_url'=>'jy_meye/book/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
				'success_url'=>'jy_meye/server_order/',
				'error_url'=>'jy_meye/server_order/',
		);
		$payid=cfc('pay')->create_pay_order($params);
		$this->pdo_update('server_order',array('pay_id'=>$payid),array('id'=>$id));
		$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
		);
		exi($res);
	}
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','main');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','main');
	}
	$order=pdo_get('jy_meye_server_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['status']){
		exi('订单已支付，无需重复支付！','error');
	}
	if($order['pay']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_meye_server_order',array('status'=>'1'),array('id'=>$order['id']));
	//计次减免
	if($order['timesorderid']){
		pdo_update('jy_meye_timeserve_order',array('lasttimes -='=>1),array('id'=>$order['timesorderid']));
	}
	if($this->plugin_exists('jy_commission')){
		//分销计入
		$order['price']=$order['pay'];
		$order['plugin_name']='jy_meye';
		model('jy_commission/commission')->create($order);
	}
	$this->model('notice')->notice_yuyueorder($order);
	exi('支付成功','success','jy_meye/server_order/list/status=1&id='.$order['id']);
}